package cn.org.itool.ws.framework.protocol.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import cn.org.itool.ws.framework.constent.WsProtocolEnum;
import cn.org.itool.ws.framework.netty.message.*;
import cn.org.itool.ws.framework.ws.WsServer;
import io.netty.buffer.ByteBuf;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Log4j2
public abstract class ProtocolService {


    protected final ObjectMapper OBJECT_MAPPER = WsServer.OBJECT_MAPPER;

    public abstract WsProtocolEnum type();

    public abstract List<BizWsPackage> decodePackage(Object text);

    public abstract BizWsMessage decodeMessage(Object message);

    public abstract Object encode(BizWsMessage resp);


    public abstract Object encode(WsHandShake resp);

    public abstract Object encode(WsHandShakeAck resp);


    public abstract Object encode(WsHeartbeat resp);


    public void decode(String text, ByteBuf buffer, List<Object> in){
        List<BizWsPackage> packageType = new ArrayList<>();
        if(StringUtils.isNotBlank(text)) {
            packageType.addAll(this.decodePackage(text));
        }
        if(Objects.nonNull(buffer)) {
            packageType.addAll(this.decodePackage(buffer));
        }

        in.addAll(packageType);
    }




}
